/**
 * Copyright (C) 2011  E-Robot
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 * @brief Map.cpp
 *
 * @date 21 nov. 2010
 * @author elca
 * @version 0.1
 */

#include "Map.h"
#include "../def.h"
#include <qdebug.h>
#include <math.h>
#include <QKeyEvent>
#include <QTime>
#define BORDER 10

Map::Map(int _nbrCellWidth, int _nbrCellHeight, int _size, int _resolution)
{

	this->size = _size;
	this->resolution = _resolution;
	this->width = _nbrCellWidth*size*resolution;
	this->height = _nbrCellHeight*size*resolution;;

	//Scene rect
	xScene = -(width/2)%size-(width/2)-size;
	yScene = -(height/2)%size-(height/2)-size;
	widthScene = 2*((width/2)%size+(width/2)+size);
	heightScene = 2*((height/2)%size+(height/2)+size);

	qDebug()<< "Width:"<<width<<" Height:"<<height<< " size:"<<size<< " reso:"<<  resolution;
	qDebug()<< "xSe:"<<xScene<<" ySe:"<<yScene<<" wiSe:"<<widthScene<<" heiSce:"<<heightScene;

	QRect sceneRect(
			xScene-BORDER,
			yScene-BORDER,
			widthScene+2*BORDER,
			heightScene+2*BORDER);

	setSceneRect(sceneRect);

	//Nombre de cellules
	nbCellHeight = heightScene/size;
	nbCellWidth = widthScene/size;
//	qDebug()<< "nbrCellWidth:"<<_nbrCellWidth<<" nbrCellHeight:"<<_nbrCellHeight<<" nbCellWidth:"<<nbCellWidth<<" nbCellHeight:"<<nbCellHeight;
//

//
//
	//Millieu du tableau de cellules
	mY = nbCellHeight/2;
	mX = nbCellWidth/2;

	qDebug()<< "millieuX:"<<mX<<" millieuY:"<<mY;


	//Création d'axe au millieu de la grille

	QGraphicsLineItem* axeX = new QGraphicsLineItem(xScene,0,xScene+widthScene,0,NULL,this);
		axeX->setPen(QPen(QBrush(Qt::black),2));
		axeX->setZValue(2);
		QGraphicsLineItem* axeY = new QGraphicsLineItem(0,yScene,0,yScene+heightScene,NULL,this);
		axeY->setPen(QPen(QBrush(Qt::black),2));
		axeY->setZValue(2);

		axe.append(axeX);
		axe.append(axeY);
}

int Map::getX(double _x){
	return (int) (mX + _x*resolution);
}

int Map::getY(double _y){
	return (int) (mY - _y*resolution);
}

void Map::keyPressEvent ( QKeyEvent * event ){
	if(event->key()==Qt::Key_1 || event->key()==Qt::Key_2|| event->key()==Qt::Key_4|| event->key()==Qt::Key_8){
		Q_EMIT(changeMask(event->key()-48));
	}

	if(event->key()==Qt::Key_Right){
		Q_EMIT(changeAlgo());
	}

	if(event->key()==Qt::Key_R){
		Q_EMIT(resetMap());
	}
	if(event->key()==Qt::Key_A){
		for(int i=0; i< axe.size();i++){
			axe.at(i)->setVisible(!axe.at(i)->isVisible());
		}
	}
}


Map::~Map()
{
	// TODO Auto-generated destructor stub
}
